package leetcode.code1697;

import java.util.Arrays;

public class Solution {

	int[] u;

	public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {
		u = new int[n];
		for (int i = 0; i < n; i++) {
			u[i] = i;
		}
		Arrays.sort(edgeList, (a, b) -> a[2] - b[2]);
		Integer[] idx = new Integer[queries.length];
		for (int i = 0; i < idx.length; i++) {
			idx[i] = i;
		}
		Arrays.sort(idx, (a, b) -> queries[a][2] - queries[b][2]);
		boolean[] ans = new boolean[queries.length];
		for (int i = 0, j = 0; i < ans.length; i++) {
			int[] query = queries[idx[i]];
			int p = query[0], q = query[1], limit = query[2];
			while (j < edgeList.length && edgeList[j][2] < limit) {
				int[] edge = edgeList[j];
				int u = edge[0], v = edge[1];
				union(u, v);
				j++;
			}
			ans[idx[i]] = sameSet(p, q);
		}
		return ans;
	}

	private boolean sameSet(int p, int q) {
		return find(p) == find(q);
	}

	private int find(int q) {
		if (u[q] != q) {
			u[q] = find(u[q]);
		}
		return u[q];
	}

	private void union(int a, int b) {
		u[find(a)] = find(b);
	}
	
	public static void main(String[] args) {
		Integer num = 200;
		Integer num1 = 200;
		System.out.println(num == num1);
		System.out.println(num1 < num);
	}
}
